/**
 * File Name   : MembersCfgService.java
 * Description : 
 */
package com.hh.admin.members.service;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.hh.admin.members.form.MemberCfgForm;
import com.hh.admin.members.form.ParamMemberBean;
import com.hh.admin.members.form.ResultMemberBean;
import com.hh.common.ibatis.dao.IQueryDao;
import com.hh.common.ibatis.dao.IUpdateDao;

/**
 * @author 冯昌硕
 * @create 2012-12-24 下午9:22:50
 */
public class MembersCfgService {
    
    private static final Logger log = Logger.getLogger(MembersCfgService.class);
    
    private IQueryDao queryDao;
    
    private IUpdateDao updateDao;

    /**
     * 查询会员信息列表
     * @param form
     * @return
     * @throws SQLException
     * @create 2012-12-24 下午9:26:56
     */
    public List<ResultMemberBean> getMembersList(MemberCfgForm form) throws SQLException {
        List<ResultMemberBean> resultLst = null;
        
        int currentPage = form.getCurrentPage();
        int formCurrentPage = currentPage;
        if(formCurrentPage <= 0) {
            currentPage = 1;
        }
        
        int pageSize = form.getPageSize();
        int offset = (currentPage - 1) * pageSize;
        log.debug("会员信息查询 : formCurrentPage=[" + formCurrentPage + "] currentPage=[" + currentPage + "] pageSize=[" + pageSize + "] offset=[" + offset + "]");
        
        ParamMemberBean paramBean = new ParamMemberBean();
        paramBean.setOffset(offset);
        paramBean.setPageSize(pageSize);
        
        resultLst = queryDao.queryForObjectList("member.selMemberList", paramBean);
        
        return resultLst;
    }
    
    /**
     * 获取会员用户总数
     * @param form
     * @return
     * @create 2012-12-24 下午9:41:20
     */
    public int getMemberTotleCnt(MemberCfgForm form) {
        int totleCnt = 0;
        
        try {
            Integer rs = queryDao.queryForObject("member.selMemberCount", Integer.class);
            if(rs != null) {
                totleCnt = rs;
            }
        } catch (ClassCastException e) {
            log.error("member.selMemberCount 查询ClassCastException异常：" + e.toString());
            e.printStackTrace();
        } catch (SQLException e) {
            log.error("member.selMemberCount 查询SQLException异常：" + e.toString());
            e.printStackTrace();
        }
        
        return totleCnt;
    }
    
    /**
     * 根据账号删除会员记录
     * @param account 会员账号
     * @return 删除影响行数
     * @throws SQLException
     * @create 2012-12-25 下午7:32:04
     */
    public int deleteOneMemberUser(String account) throws SQLException {
        return updateDao.delete("member.deleteMemberUser", account);
    }
    
    /**
     * 批量删除会员用户
     * @param accounts 要删除的会员账号
     * @return
     * @throws SQLException
     * @create 2012-12-25 下午7:54:59
     */
    public int deleteManyMemberUser(String[] accounts) throws SQLException {
        return updateDao.batchDelete("member.deleteMemberUser", accounts);
    }
    
    /**
     * 获取会员详情
     * @param account 会员账号
     * @return
     * @throws SQLException 
     * @throws ClassCastException 
     * @create 2012-12-25 下午8:15:59
     */
    public ResultMemberBean getDetail(String account) throws ClassCastException, SQLException {
        return queryDao.queryForObject("member.selMemberDetail", account, ResultMemberBean.class);
    }

    public void setQueryDao(IQueryDao queryDao) {
        this.queryDao = queryDao;
    }

    public void setUpdateDao(IUpdateDao updateDao) {
        this.updateDao = updateDao;
    }
    
}
